<?php
/**
 * 物品控制器
 * 
 * @category QuickBug
 * @copyright http://www.vquickbug.com
 */
class DeptController extends BaseController
{
	/**
	 * 模型
	 *
	 * @var unknown_type
	 */
	private $model;
	
	/**
	 * 自动运行
	 */
	public function init(){
		parent::init();
		$this->model = new Model_Dept();
	}

	/**
	 * 首页
	 */
	public function listAction(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$offset = ($page-1)*$rows;
		$result = array();
		$result["total"] = $this->model->countDept();
		$result["rows"] = $this->model->listDept($offset,$rows);
		
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function combotreeAction(){
		$result = array();
		$result = $this->model->listDept();
		
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function addAction(){
		$name = $this->request->getPost('name');
		$result = $this->model->addDept($name);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'添加部门失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function updateAction(){
		$id = intval($_REQUEST['id']);
		$name = $_REQUEST['name'];
		$result = $this->model->updateDept($id,$name);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'编辑部门失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function delAction(){
		$id = intval($_REQUEST['id']);
		$result = $this->model->delDept($id);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'删除部门失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
}